import { Controller } from 'egg';

class EvaluateController extends Controller {
    /**
     * 新增评论
     */
    public async createEvaluate() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                employee_id: 'string',
                target_employee_id: 'string',
                evaluate_content:'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.evaluateService.createEvaluate(request.body);
        ctx.body = ctx.helper.responseSuccess('创建成功');
    }

    /**
     * 删除评论
     */
    public async deleteEvaluate() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                evaluate_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.evaluateService.deleteEvaluate(request.body);
        ctx.body = ctx.helper.responseSuccess('删除成功');
    }

    /**
     * 查找部门职员及评价列表
     */
    public async searchEvaluateList() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                work_unit_id: 'string',
                department_id: 'string',
                employee_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.evaluateService.searchEvaluateList(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 根据评论id查找评论内容
     */
    public async searchEvaluateById() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                evaluate_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.evaluateService.searchEvaluateById(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 修改评论内容
     */
    public async updateEvaluate() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                evaluate_id: 'string',
                evaluate_content: 'string',
                evaluate_picture: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.evaluateService.updateEvaluate(request.body);
        ctx.body = ctx.helper.responseSuccess('修改成功');
    }

    /**
     * 根据职员id查找评论内容
     */
    public async searchEvaluateByEmployeeId() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                target_employee_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.evaluateService.searchEvaluateByEmployeeId(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }
}
export default EvaluateController;